package com.liming.admin.controller;

import com.liming.auth.client.AdminAuthClient;
import com.liming.common.anotation.AnyPermissionAuth;
import com.liming.common.pojo.dto.authinfo.AdminAddOrUpdateDTO;
import com.liming.common.pojo.dto.authinfo.AdminLoginDTO;
import com.liming.common.pojo.dto.authinfo.UserPageQuery;
import com.liming.common.pojo.entity.authinfo.Region;
import com.liming.common.pojo.vo.PageVO;
import com.liming.common.pojo.vo.authinfo.*;
import com.liming.common.result.LMResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liming
 * @date 2025/1/2
 */
@Slf4j
@Validated
@RestController
@AllArgsConstructor
@Tag(name = "权限信息接口-Web")
@RequestMapping("/admin/info")
public class AuthInfoController {

    private final AdminAuthClient adminAuthClient;

    @Operation(
            summary = "列表 - 管理员",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = AdminVO.class))
                    })
            }
    )
    @GetMapping("list")
    @AnyPermissionAuth(permissions = {"All"})
    public LMResult<List<AdminVO>> list() {
        return LMResult.success(adminAuthClient.list());
    }

    @Operation(
            summary = "登录 - 管理员权限信息",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = AdminAuthVO.class))
                    })
            }
    )
    @GetMapping("auth")
    public LMResult<AdminAuthVO> auth() {
        return LMResult.success(adminAuthClient.auth());
    }

    @Operation(
            summary = "列表 - 角色列表",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = RoleVO.class))
                    })
            }
    )
    @GetMapping("roleList")
    public LMResult<List<RoleVO>> roleList() {
        return LMResult.success(adminAuthClient.roleList());
    }

    @Operation(
            summary = "列表 - 权限列表",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = PermissionVO.class))
                    })
            }
    )
    @GetMapping("permissionList")
    public LMResult<List<PermissionVO>> permissionList() {
        return LMResult.success(adminAuthClient.permissionList());
    }

    @Operation(
            summary = "列表 - 管理员对应角色列表",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = RoleVO.class))
                    })
            }
    )
    @GetMapping("adminRole/{adminId}")
    public LMResult<List<RoleVO>> adminRole(@PathVariable Long adminId) {
        return LMResult.success(adminAuthClient.adminRoleList(adminId));
    }

    @Operation(
            summary = "列表 - 角色对应权限列表",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = PermissionVO.class))
                    })
            }
    )
    @GetMapping("rolePermission/{roleId}")
    public LMResult<List<PermissionVO>> rolePermission(@PathVariable Long roleId) {
        return LMResult.success(adminAuthClient.rolePermissionList(roleId));
    }

    @Operation(
            summary = "列表 - 地区列表",
            parameters = {
                    @Parameter(name = "parentId", description = "父级ID", example = "1"),
                    @Parameter(name = "level", description = "等级", example = "1")
            },
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = Region.class))
                    })
            }
    )
    @GetMapping("regionList")
    public LMResult<List<Region>> regionList(@NotNull @RequestParam Long parentId, @NotNull @RequestParam Integer level) {
        return LMResult.success(adminAuthClient.regionList(parentId, level));
    }

    @Operation(
            summary = "分页 - 用户列表",
            responses = {
                    @ApiResponse(responseCode = "1", description = "获取成功", content = {
                            @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = UserVO.class))
                    })
            }
    )
    @GetMapping("userPage")
    public LMResult<PageVO<UserVO>> userPage(UserPageQuery query) {
        return LMResult.success(adminAuthClient.userPage(query));
    }

    @Operation(
            summary = "登录 - 用户名|手机号",
            responses = {
                    @ApiResponse(responseCode = "1", description = "登录成功", content = {
                            @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = String.class))
                    })
            }
    )
    @PostMapping("login")
    public LMResult<AdminLoginVO> login(AdminLoginDTO adminLoginDTO) {
        return LMResult.success(adminAuthClient.login(adminLoginDTO));
    }

    @Operation(
            summary = "登出",
            responses = {
                    @ApiResponse(responseCode = "1", description = "登出成功", content = {
                            @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = Boolean.class))
                    })
            }
    )
    @PostMapping("logout")
    public LMResult<Boolean> logout() {
        return LMResult.success(adminAuthClient.logout());
    }

    @Operation(
            summary = "新增 - 角色",
            parameters = {
                    @Parameter(name = "roleName", description = "角色名称", required = true)
            }
    )
    @PostMapping("role")
    public LMResult<Boolean> role(@NotBlank(message = "角色名称不能为空") @RequestParam String roleName) {
        return LMResult.success(adminAuthClient.role(roleName));
    }

    @Operation(
            summary = "新增 - 权限",
            parameters = {
                    @Parameter(name = "permissionName", description = "权限名称", required = true)
            }
    )
    @PostMapping("permission")
    public LMResult<Boolean> permission(@NotBlank(message = "权限名称不能为空") @RequestParam String name, @RequestParam String description) {
        return LMResult.success(adminAuthClient.permission(name, description));
    }

    @Operation(
            summary = "新增 - 管理员角色",
            parameters = {
                    @Parameter(name = "adminId", description = "管理员ID", required = true),
                    @Parameter(name = "roleIds", description = "角色ID列表", required = true)
            }
    )
    @PostMapping("adminRole/{adminId}")
    public LMResult<Boolean> adminRole(@PathVariable Long adminId, @RequestBody List<Long> roleIds) {
        return LMResult.success(adminAuthClient.adminRole(adminId, roleIds));
    }

    @Operation(
            summary = "新增 - 角色权限",
            parameters = {
                    @Parameter(name = "roleId", description = "管理员ID", required = true),
                    @Parameter(name = "permissionIds", description = "角色ID列表", required = true)
            }
    )
    @PostMapping("rolePermission/{roleId}")
    public LMResult<Boolean> rolePermission(@PathVariable Long roleId, @RequestBody List<Long> permissionIds) {
        return LMResult.success(adminAuthClient.rolePermission(roleId, permissionIds));
    }

    @Operation(summary = "新增 - 管理员")
    @PostMapping
    public LMResult<Boolean> add(@RequestBody AdminAddOrUpdateDTO adminAddOrUpdateDTO) {
        return LMResult.success(adminAuthClient.addInfo(adminAddOrUpdateDTO));
    }

    @Operation(summary = "更新 - 管理员信息")
    @PutMapping
    public LMResult<Boolean> update(@RequestBody AdminAddOrUpdateDTO adminAddOrUpdateDTO) {
        return LMResult.success(adminAuthClient.updateInfo(adminAddOrUpdateDTO));
    }

    @Operation(summary = "更新 - 管理员账号状态 0未锁定 1锁定")
    @PutMapping("locked/{id}")
    public LMResult<Boolean> updateStatus(@PathVariable Long id) {
        return LMResult.success(adminAuthClient.updateStatus(id));
    }

    @Operation(summary = "更新 - 用户激活状态 0未激活 1激活")
    @PutMapping("userActive/{id}")
    public LMResult<Boolean> updateUserActive(@PathVariable Long id) {
        return LMResult.success(adminAuthClient.updateUserActive(id));
    }

    @Operation(summary = "更新 - 用户锁定状态 0未锁定 1锁定")
    @PutMapping("userLocked/{id}")
    public LMResult<Boolean> updateUserLocked(@PathVariable Long id) {
        return LMResult.success(adminAuthClient.updateUserLocked(id));
    }

    @Operation(summary = "删除 - 管理员")
    @DeleteMapping("{id}")
    public LMResult<Boolean> delete(@PathVariable Long id) {
        return LMResult.success(adminAuthClient.delete(id));
    }

    @Operation(summary = "删除 - 角色")
    @DeleteMapping("deleteRole/{id}")
    public LMResult<Boolean> deleteRole(@PathVariable Long id) {
        return LMResult.success(adminAuthClient.deleteRole(id));
    }

    @Operation(summary = "删除 - 权限")
    @DeleteMapping("deletePermission/{id}")
    public LMResult<Boolean> deletePermission(@PathVariable Long id) {
        return LMResult.success(adminAuthClient.deletePermission(id));
    }
}
